﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DungChung;
namespace CoVuaServer
{
    class TaiKhoanDAO : DAO
    {
        public TaiKhoanDAO(DataProvider dataProvider)
        {
            _dataProvider = dataProvider;
        }
        protected override object GetDataFromRow(DataTable dataTable, int index)
        {
            TaiKhoanDTO taiKhoan = new TaiKhoanDTO();
            taiKhoan.UserName = dataTable.Rows[index]["UserName"].ToString();
            taiKhoan.Password = dataTable.Rows[index]["Password"].ToString();
            taiKhoan.TenHienThi = dataTable.Rows[index]["TenHienThi"].ToString();
            taiKhoan.NgaySinh = Convert.ToDateTime(dataTable.Rows[index]["NgaySinh"]);
            taiKhoan.GioiTinh = dataTable.Rows[index]["GioiTinh"].ToString();
            taiKhoan.Status = dataTable.Rows[index]["Status"].ToString();
            taiKhoan.KinhNghiem = Convert.ToInt32(dataTable.Rows[index]["KinhNghiem"]);
            taiKhoan.GioiThieu = dataTable.Rows[index]["GioiThieu"].ToString();
            return (object)taiKhoan;
        }
        public List<TaiKhoanDTO> GetAllTaiKhoan(string query)
        {
            List<TaiKhoanDTO> taiKhoans = new List<TaiKhoanDTO>();
            DataTable dataTable = _dataProvider.Execute(query);
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                taiKhoans.Add((TaiKhoanDTO)GetDataFromRow(dataTable, i));
            }
            return taiKhoans;
        }
        public void AddTaiKhoanMoi(TaiKhoanDTO taiKhoan)
        {
            string query = "insert into TAIKHOAN values('" + taiKhoan.UserName + "','" + taiKhoan.Password + "',N'" +
                taiKhoan.TenHienThi + "',N'" + taiKhoan.GioiTinh + "','" + taiKhoan.NgaySinh.ToString() + "',N'" +
                taiKhoan.GioiThieu + "',N'" + taiKhoan.Status + "',N'" + taiKhoan.KinhNghiem.ToString() + "')";
            _dataProvider.ExecuteNonQuery(query);
        }
        public void UpdateDiem(TaiKhoanDTO taiKhoan)
        {
            string query = "update TAIKHOAN set KinhNghiem = " + taiKhoan.KinhNghiem + " where UserName = '" + taiKhoan.UserName + "'";
            _dataProvider.ExecuteNonQuery(query);
        }
        public TaiKhoanDTO[] Lay20NguoiDiemCao()
        {
            TaiKhoanDTO[] taiKhoans = new TaiKhoanDTO[20];
            List<TaiKhoanDTO> taiKhoan = new List<TaiKhoanDTO>();
            taiKhoan = GetAllTaiKhoan("select * from TAIKHOAN order by KinhNghiem desc");
            if (taiKhoan.Count < 20)
            {
                for (int i = 0; i < taiKhoan.Count; i++)
                {
                    taiKhoans[i] = taiKhoan[i];
                }
                for (int i = taiKhoan.Count; i < 20; i++)
                {
                    TaiKhoanDTO tk = new TaiKhoanDTO();
                    tk.TenHienThi = "-------";
                    tk.UserName = "--------";
                    tk.Status = string.Empty;
                    tk.GioiThieu = string.Empty;
                    tk.KinhNghiem = 0;
                    taiKhoans[i] = tk;
                }
            }
            else
            {
                for (int i = 0; i < 20; i++)
                {
                    taiKhoans[i] = taiKhoan[i];
                }
            }
            return taiKhoans;
        }
    }
}
